package com.lucun.es_transaction.service.impl;
import com.lucun.es_transaction.entity.Classification;
import com.lucun.es_transaction.entity.Commodity;
import com.lucun.es_transaction.entity.Orders;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.ClassificationMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.ClassificationService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class ClassificationServiceImpl implements ClassificationService {
    @Autowired
    ClassificationMapper classificationMapper;
    @Autowired
    UserLogMapper userLogMapper;
    @Override
    public JsonResult<?> insert(Classification classification, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(classification)){
            throw  new BaseException("数据不能为空");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        classification.setCreateTime(new Date());//创建时间
        int i = classificationMapper.insertSelective(classification);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte)0);
        log.setUserId(1);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+idByToken+"在"+time+"添加"+classification.getCategory()+"商品分类"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+idByToken+"在"+time+"添加"+classification.getCategory()+"商品分类"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","添加成功",classification);
    }

    @Override
    public JsonResult<?> deleteByPrimaryKey(Classification id, HttpServletRequest request) {
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        Classification c=classificationMapper.selectByPrimaryKey(id);

        int i = classificationMapper.deleteByPrimaryKey(id);
        UserLog log = new UserLog();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());
        log.setLogType((byte) 0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(i)) {
            log.setLogStatus((byte) 1);
            log.setLogName("账号" + numberByToken + "在" + time + "删除" + c.getCategory() + "商品分类" + "失败");
            userLogMapper.insert(log);
            return new JsonResult("200", "删除失败");
        } else {
            log.setLogStatus((byte) 0);
            log.setLogName("账号" + numberByToken + "在" + time + "查询" + c.getCategory() + "订单" + "成功");
            return new JsonResult("200", "删除成功");

        }
    }
    @Override
    public JsonResult<?> updateByPrimaryKey(Classification classification, HttpServletRequest request) {
        return null;
    }
    @Override
    public JsonResult<?> updateByPrimaryKeySelective(Classification classification, HttpServletRequest request) {
        classification.setUpdateTime(new Date());
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        int i=classificationMapper.updateByPrimaryKeySelective(classification);
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (i > 0){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+classification.getCategory()+"商品分类"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+classification.getCategory()+"商品分类"+"失败");
            userLogMapper.insert(log);
        }
        return new JsonResult("200","修改成功",classification);
    }
    @Override
    public JsonResult<?> selectByPrimaryKey(Classification classification, HttpServletRequest request) {
        Classification i=classificationMapper.selectByPrimaryKey(classification);
        int idByToken = JwtUtil.getIdByToken(request);
        String numberByToken = JwtUtil.getNumberByToken(request);
        if(ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        if(ObjectUtils.isEmpty(numberByToken)){
            throw new BaseException("解析token失败");
        }
        UserLog log=new UserLog();
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.setCreateTime(new Date());

        log.setLogType((byte)0);
        log.setUserId(idByToken);
        if (ObjectUtils.isEmpty(classification)){
            log.setLogStatus((byte)1);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+classification.getCategory()+"商品分类"+"成功");
            userLogMapper.insert(log);
        } else {
            log.setLogStatus((byte)0);
            log.setLogName("账号"+numberByToken+"在"+time+"修改"+classification.getCategory()+"商品分类"+"失败");
            userLogMapper.insert(log);
        }
        if (ObjectUtils.isEmpty(classification)){
            throw  new BaseException("数据查询失败");
        }
        return new JsonResult("200","查询成功",i);
    }
}
